<?php
namespace BoxEdu\Widget;
class IndexWidget extends \WidgetController {
	/*****学员学员心声****/
	public function order_comment($where, $limit = 5) {
		//$data = BoxModel("sns_topic")->get($p['where'], $p['limit']?$p['limit']:10,"lastupdate desc");

		$where['type'] = "orderComment";
		$where['is_show'] = 1;
		$this->subject_comment = BoxModel("sns_topic")->get($where, 30, "lastupdate desc");
		$this->show_page($this->subject_comment['pager']);
		$this->display();
	}

	/******课程表******/
	public function subject_list($param) {
		$subject_id = $param['subjectid'];
		$this->url = $param['url'];
		$this->show_down = $param['show_down'];
		$sections = BoxModel("edu_sections")->get(array('sectionsubjectid' => $subject_id, 'status' => 1, 'is_show' => 1), "", "order_by");
		$model = BoxModel("edu_knows");
		foreach ($sections as $k => $one) {
			$sections[$k]['knows'] = $model->get(array('knowssectionid' => $one['sectionid']), "", "order_by");
			$modelKnowsUser = BoxModel("edu_knows_user");
			foreach ($sections[$k]['knows'] as $k1 => $one) {
				if ($this->user_info['user_id']) {
					$sections[$k]['knows'][$k1]['history'] = $modelKnowsUser->where(array('user_id' => $this->user_info['user_id'], 'knowsid' => $one['knowsid']))->find();
				}
			}
		}
		//dump($sections);
		$this->sections = $sections;
		if ($this->knowsid = I('knowsid')) {
			$this->knowsData = BoxModel("edu_knows")->load($this->knowsid);
		}
		if ($this->show_down) {
			//  $subjectInfo=BoxModel("edu_subject")->where(array('subjectid'=>$subject_id))->find();
			// $class_id=$subjectInfo['subject_class'];
			$this->downList = BoxModel("edu_attach")->get(array('class_id' => $subject_id));
			// dump($this->downList);
		}
		$this->display();
	}

	//我的课程
	public function my_subject($type) {
		$user_id = I('user_id', $this->user_info['user_id'], "intval");
		$user_info = Logic("BoxEdu", "User")->info($user_id);
		$data = $user_info['subjectids_info'];
		$learning_subject = array();
		$over_subject = array();
		//dump($user_info['subjectids_info']);
		foreach ($data as $k => $one) {
			$subjectid = $one['subjectid'];
			// dump($this->orderComment);
			$data[$k]['comment_str'] = $r['error'] == 0 ? "学员心声" : "评论";
			if ($one['type'] == 2) {
				$data[$k]['type_str'] = "试听";
			} elseif ($one['type'] == 1) {
				$data[$k]['type_str'] = "进入班级";
			} else {
				$data[$k]['type_str'] = "开通课程";
			}
			$one['end_date_count'] = intval((strtotime($one['end_date']) - time()) / (3600 * 24));
			$over_subject[$k] = $one;
			if ($one['end_date'] < date("Y-m-d")) {
				$over_subject[$k]['over'] = 1;
			} else {
				$learning_subject[] = $one;
			}

			$subs[] = $one['subjectid'];
		}

		if ($type == 'scholarship') {
//申请奖学金已
			foreach ($over_subject as $k => $one) {
				$subjectid = $one['subjectid'];
				$r = BoxModel("edu_user_scholarship")->where("user_id='$user_id' and subject_id='$subjectid'")->find();
				$one['remark'] = $r['remark'];
				$one['status_test'] = "";
				$one['id'] = $r['id'];
				if ($r['status'] == 1) {
					$one['status_test'] = "等待审核";
				}
				if ($r['status'] == 2) {
					$one['status_test'] = "通过审核";
				}
				if ($r['status'] == 3) {
					$one['status_test'] = "申请未通过";
				}
				if ($r['status'] == 4) {
					$one['status_test'] = "奖学金已发放";
				}
				$over_subject[$k] = $one;
			}
			$this->data = $over_subject;
		} elseif ($type == 'over') {
//申请重学
			foreach ($over_subject as $k => $one) {
				$subjectid = $one['subjectid'];
				if ($one['over'] != 1) {
					unset($over_subject[$k]);
				}
			}
			$this->data = $over_subject;

		} elseif ($type == 'relearn') {
//申请重学
			foreach ($over_subject as $k => $one) {
				$subjectid = $one['subjectid'];
				$r = BoxModel("edu_user_relearn_apply")->where("user_id='$user_id' and subject_id='$subjectid'")->find();
				//dump("user_id='$this->user_id' and subject_id='$subjectid'");
				$one['id'] = $r['id'];
				$one['remark'] = $r['remark'];
				$one['status_test'] = "";
				if ($r['status'] == 1) {
					$one['status_test'] = "等待审核";
				}
				if ($r['status'] == 2) {
					$one['status_test'] = "通过审核";
				}
				if ($r['status'] == 3) {
					$one['status_test'] = "申请未通过";
				}
				if ($r['status'] == 4) {
					$one['status_test'] = "已开通";
				}
				$over_subject[$k] = $one;
			}
			$this->data = $over_subject;

		} elseif ($type == 'learning') {
//学习中的课程
			$model = BoxModel("edu_subject");
			foreach ($learning_subject as $k => $one) {
				$learning_subject[$k]['orderComment'] = BoxModel("sns_topic")->where(array('user_id' => $this->user_info['user_id'], 'pid' => $one['subjectid'], 'type' => 'orderComment'))->find();
				$learning_subject[$k]['children'] = $model->where(array('parent_id' => $one['subjectid']))->find();
			}
			$this->data = $learning_subject;
		} else {
//继续购买
			$sub_exp = trim(implode(",", $subs));
			if ($sub_exp) {
				$data = BoxModel("EduSubject")->get("is_show=1  and subjectid not in($sub_exp)", 5);
			} else {
				$data = BoxModel("EduSubject")->get("is_show=1 ", 5);
			}

			$this->data = $data['list'];
		}
		$this->type = $type;
		$this->display();
	}

	/****订单列表***/
	public function order_list($where, $limit = "20") {
		$data = BoxModel("edu_subject_order")->get($where, $limit);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/*****学员动态****/
	public function timeline($param) {
		$where = $param['where'] ? $param['where'] : 1;
		$limit = $param['limit'] ? $param['limit'] : 10;
		//	dump($where);
		$data = BoxModel("SystemLog")->get($where, $limit);
		$model = BoxModel("system_log_template");
		$userModel = BoxModel("system_member");
		$from = dict("drive_from");
		foreach ($data['list'] as $k => $one) {
			$tplData = $model->where(array('event' => $one['event']))->find();
			$one['icon'] = $tplData['icon'] ? $tplData['icon'] : 'glyphicon glyphicon-gift';
			$one['from_str'] = $from[$one['from']];
			$one['user'] = $userModel->load($one['user_id']);
			$data['list'][$k] = $one;
		}
		$this->LogData = $data['list'];
		$this->show_page($data['pager']);
		$this->backColor = array(
			'xuebazi' => 's_c',
			'ask_num' => 'p_c',
			'money' => 'i_c',
			'online_pay' => 'w_c',
		);
		$this->display();
	}

	//排行榜
	public function rank($param) {
		//dump($param);
		$where = $param['where'];
		$limit = $param['limit'];
		/******排行榜**/
		$data = BoxModel('edu_user_scholarship')->get($where, $limit, "order_by");
		$memberModel = BoxModel("system_member");
		foreach ($data['list'] as $k => $one) {
			$data['list'][$k]['user'] = $memberModel->load($one['user_id']);
		}
		//dump($data['list']);
		$this->rankData = $data['list'];
		$this->display();
	}

	/******用户等级***/
	public function user_tag($user_id) {

		return "正式会员";

	}

	/******赚学币推广页面***/
	public function zhuan() {
		$this->qu_all = (int) BoxModel("sns_topic")->where(array('is_show' => 1, 'is_best' => '0', 'type' => 'EduAsk'))->count();
		$school_id = $this->user_info['school_id'];
		$now = time();
		$list = (array) BoxModel("edu_user_agent_apply")->get("school_id='$school_id' and status in (1,2)");
		foreach ($list as $k => $one) {
			if ($one['expirationTime'] > 0 && $one['expirationTime'] < $now) {
				unset($list[$k]);
			}

		}
		$count = count($list);
		$shool_info = BoxModel("edu_schools")->where("id='$school_id'")->find();
		$this->agentLimit = $shool_info['agentLimit'] - $count;
		//可领取的学币
		$red_packets = BoxModel("edu_red_packets")->get(array('user_id' => $this->user_info['user_id'], 'status' => 1), 5);
		$this->red_packets = $red_packets['list'];
		$this->display();
	}
}